package com.wlz.demo;

import com.wlz.curd.impl.PE;

import java.util.*;

/**
 * 入口类
 */
public class Main {

    private final static List<Employee> employeeMap = new ArrayList<Employee>();

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        Boolean flag = true;
       while (flag) {
           sout();
           System.out.println("请选择：");
           int input = scanner.nextInt();
           switch (input){
               case 1:
                   // 新增员工
                   insert(scanner);
                   flag = true;
                   break;
               case 2:
                   query();
                   System.out.println("输入0返回");
                   Integer next = scanner.nextInt();
                   if (next.equals(0)) {
                       flag = true;
                   }else {
                       flag = false;
                   }
                   break;
               case 3:
                   System.out.println("请输入要删除的员工编号");
                   delete(scanner);
                   flag = true;
                   break;
               case 4:
                   flag = false;
                   break;
               default:
                   flag = false;
                   System.exit(0);
       }

        }


    }


    public static void sout() {
        System.out.println("1- 1-新增员工\n" +
                "2-查看员工\n" +
                "3-删除员工\n" +
                "4-退出");
    }

    /**
     * 新增员工
     * @param scanner
     */
    public static void insert(Scanner scanner) {
        System.out.println("请输入员工编号:");
        Integer id = scanner.nextInt();
        System.out.println("输入员工姓名:");
        String name = scanner.next();
        System.out.println("请输入员工基本工资:");
        Double basicWage = scanner.nextDouble();
        System.out.println("请选择员工岗位(1—项目经理  2—程序员): 1:");
        Integer type = scanner.nextInt();
        if (type == 1) {
            PEmployee employee = new PEmployee();
            System.out.println("请输入项目奖金:");
            Double projectBonus = scanner.nextDouble();
            employee.setProjectBonus(projectBonus);
            employee.setType(1);
            employee.setId(id);
            employee.setName(name);
            employee.setBasicWage(basicWage);
            employeeMap.add(employee);
        }else if (type == 2) {
            SEmployee employee = new SEmployee();
            System.out.println("请输入工作年限:");
            Integer projectBonus = scanner.nextInt();
            employee.setWorkingYears(projectBonus);
            employee.setType(2);
            employee.setId(id);
            employee.setName(name);
            employee.setBasicWage(basicWage);
            employeeMap.add(employee);
        }
        System.out.println("新增完毕");
        System.out.println("是否继续(Y/N):N");
        String next = scanner.next();
        if (next.equalsIgnoreCase("y")) {
            insert(scanner);
        }
    }

    /**
     * 查看员工
     */
    public static void query() {
        System.out.println("员工编号   \t\t 员工姓名   \t\t 岗位    \t\t工资");
        for (Employee employee:employeeMap) {
            System.out.println(employee.getId() + "\t\t" + employee.getName() + "\t\t" + employee.getBasicWage() + "\t\t" + employee.calculateSalary());
        }
    }

    /**
     * 删除员工
     * @param scanner
     */
    public static void delete(Scanner scanner) {
        System.out.println("请输入要删除的员工编号:");
        int id = scanner.nextInt();

        for (Employee employee:employeeMap) {
            if (employee != null) {
                if (employee.getId().equals(id)) {
                    System.out.println("你要删除的员工信息为:");
                    System.out.println("员工编号:"+employee.getId());
                    System.out.println("员工姓名:"+employee.getName());
                    if (employee.getType().equals(1)) {
                        System.out.println("岗位:项目经理");
                    }else if (employee.getType().equals(2)) {
                        System.out.println("岗位:程序员");
                    }
                    System.out.println("基本工资:"+employee.getBasicWage());
                    if (employee.getType().equals(2)) {
                    System.out.println("工作年限:"+ ((SEmployee) employee).getWorkingYears());
                    }else if (employee.getType().equals(1)) {
                    System.out.println("项目奖金:"+((PEmployee) employee).getProjectBonus());
                    }
                }
                System.out.println("否确定(Y/N):");
                String next = scanner.next();
                if ("y".equalsIgnoreCase(next)) {
                    employeeMap.remove(employee);
                    System.out.println("删除成功!");
                }else if ("n".equalsIgnoreCase(next)) {
                    System.out.println("删除失败!");
                }
                break;
            }
        }
    }

}
